import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './router'
import store from '@/store'
import iView from 'view-design'
import {
  ifload
} from '@/api/api'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start()
  let isLogin = store.state.admin.isLogin
  if (isLogin === false) {
    ifload().then(info => {
      if (info.data.code === '201') {
        if(info.data.data === '教师'){
          next()
        }else{
          if (to.name == 'sign_in') {
            next()
          } else{
          iView.Message.error('权限不足，请登录')
          next({
            name: 'sign_in'
          })
        }
        }
      } else {
        if (to.name == 'sign_in') {
          next()
        } else {
          iView.Message.error('请先登录')
          next({
            name: 'sign_in'
          })
        }
        iView.LoadingBar.finish()
        window.scrollTo(0, 0)
      }
    })

  } else {
    next()
  }
})

router.afterEach(() => {
  iView.LoadingBar.finish()
  window.scrollTo(0, 0)
})


export default router